package Day2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo3")
public class demo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
       //获取请求方式get post
        String method = req.getMethod();
        System.out.println(method);
        //获取协议版本号
        String protocol = req.getProtocol();
        System.out.println(protocol);
        //获取ip地址
        String remoteAddr = req.getRemoteAddr();
        System.out.println(remoteAddr);
        //获取servlet虚拟路径
        String servletPath = req.getServletPath();
        System.out.println(servletPath);
        //获取项目访问路径
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
       //获取url
        StringBuffer url = req.getRequestURL();
        System.out.println(url.toString());
      //获取uri
        String uri = req.getRequestURI();
        System.out.println(uri);
      //获取请求参数
        String query = req.getQueryString();
        System.out.println(query);
      //获取请求头
        //浏览器版本
        String header = req.getHeader("user-agent");
        System.out.println(header);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
//        BufferedReader reader = req.getReader();
//        String s = reader.readLine();
//        System.out.println(s);
//        doGet(req,resp);
        req.setCharacterEncoding("utf-8");
        //获取所有参数Map集合
//        Map<String,String[]> reaqA =req.getParameterMap();

        //根据名称获取请求参数
        String[] names = req.getParameterValues("name");
        for (String name : names) {
            System.out.println("name :"+name);
        }
    }
}
